﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SmartCard.GSM_11_11;
using SmartCard.Framework;

namespace SmartCard.GSM_11_11.UnitTests
{
	[TestClass]
	public class CardIntegrationTests
	{
		[TestMethod]
		public void GetCardInstanceIdentifier()
		{
			// get the list of all readers, and find the reader that has a card
			// inserted in it.  we will currently develop directly for the dekart
			// reader we are testing against.
			var manager = new DefaultFrameworkCardReaderManager();
			var dekartReader = GetDekartReader(manager);

			// verify the reader exists
			Assert.IsNotNull(dekartReader, "No dekart SIM reader found, is it inserted in the PC?");
			
			// now get the card from the reader
			var card = dekartReader.GetInsertedCard();
			Assert.IsNotNull(card, "No card in dekart reader.  SIM card must be in reader");
	
			// creat the feature class
			var features = new Gsm_11_11_Features(card);

			try
			{
				card.Connect(new ConnectionOptions());
				var icc = features.GetIcc();
				Assert.IsTrue(features.IsSupported());
				Assert.IsNotNull(icc.Raw);
				Assert.AreEqual(10, icc.Raw.Length);
			}
			finally
			{
				card.Disconnect(new DisconnectOptions());
			}
		}

		#region privates

		private ISmartCardReader GetDekartReader(ISmartCardReaderManager manager)
		{
			var reader = manager.ManagedSmartCardReaders.Where((x) =>
				x.FriendlyName.StartsWith("Dekart Smart Card Reader ")).FirstOrDefault();

			return reader;
		}

		#endregion
	}
}
